﻿using System.Windows.Forms;
using DesignerHelper.Addons;
using SpreadsheetGear;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    public partial class Footer : UserControl, IScv<IPageSetup>
    {
        public Footer()
        {
            InitializeComponent();
        }

        public Footer(IPageSetup ps) : this()
        {
            TriggerInit(ps);
        }

        private IPageSetup PageSetup { get; set; }

        #region 封装变量

        private string CFooter
        {
            get { return textBoxCenterFooter.Text; }
            set { textBoxCenterFooter.Text = value; }
        }

        private string LFooter
        {
            get { return textBoxLeftFooter.Text; }
            set { textBoxLeftFooter.Text = value; }
        }

        private string RFooter
        {
            get { return textBoxRightFooter.Text; }
            set { textBoxRightFooter.Text = value; }
        }

        #endregion

        #region interface

        Control IScv<IPageSetup>.GetControl()
        {
            return this;
        }

        void IScv<IPageSetup>.TriggerReset()
        {
            TriggerReset();
        }

        void IScv<IPageSetup>.TriggerSet()
        {
            TriggerSet();
        }

        #endregion

        public void TriggerInit(IPageSetup ps)
        {
            PageSetup = ps;
            Init();
        }

        public void TriggerSet()
        {
            Set();
        }

        public void TriggerReset()
        {
            Init();
        }

        private void Init()
        {
            IPageSetup ps = PageSetup;
            CFooter = ps.CenterFooter;
            LFooter = ps.LeftFooter;
            RFooter = ps.RightFooter;
        }

        private void panelAllText_Resize(object sender, System.EventArgs e)
        {
            int h = (this.Height
                     - panelLeftFooter.Margin.Top - panelLeftFooter.Margin.Bottom
                     - panelCenterFooter.Margin.Top - panelCenterFooter.Margin.Bottom
                     - panelRightFooter.Margin.Top - panelRightFooter.Margin.Bottom)/3;

            panelRightFooter.Height = panelCenterFooter.Height = panelLeftFooter.Height = h;

            panelCenterFooter.Location = new System.Drawing.Point(panelCenterFooter.Margin.Left,
                Height - panelLeftFooter.Margin.Top - h
                - panelLeftFooter.Margin.Bottom - panelCenterFooter.Margin.Top);

            panelRightFooter.Location = new System.Drawing.Point(panelRightFooter.Margin.Left,
                panelCenterFooter.Location.Y - h
                - panelCenterFooter.Margin.Bottom - panelRightFooter.Margin.Top);
        }

        private void Set()
        {
            IPageSetup ps = PageSetup;
            ps.CenterFooter = CFooter;
            ps.LeftFooter = LFooter;
            ps.RightFooter = RFooter;
        }
    }
}